package Practice;

class Solution {//合并两个有序数组
    public void merge(int[] nums1, int m, int[] nums2, int n){
        for(int i = 0; i < n; i++) {
            nums1[m + i] = nums2[i];//把num2的元素先添加到num1里
        }

        for(int i = 0; i < nums1.length - 1; i++) {
            for(int j = 0; j < nums1.length - i - 1; j++) {
                if(nums1[j] > nums1[j + 1]) {
                    int tmp = nums1[j];
                    nums1[j] = nums1[j + 1];
                    nums1[j + 1] = tmp;
                }
            }
        }
    }

    public int removeDuplicates(int[] num) {//删除有序数组的重复项
        int length = num.length;
        for (int i = 0; i < length; i++) {
            if(i != 0 && num[i] == num[i - 1]) {
                //delete
                for(int j = i; j < length - 1; j++) {
                    num[j] = num[j + 1];
                }
                num[num.length - 1] = 0;
                length--;
                i--;//这样不会越界
            }
        }
        return length;
    }

    public int removeElement(int[] nums, int val) {
        int sum = nums.length;
        for(int i = 0; i < sum; i++) {
            if(nums[i] == val) {//相同就删除
                for(int j = i; j < sum - 1; j++) {
                    nums[j] = nums[j + 1];
                }
                sum--;
                i--;
            }
        }
        return sum;
    }
}
public class text{
    public static void main(String[] args) {
        int[] num = {1, 2, 2, 4};
        Solution sl = new Solution();
        int length = sl.removeElement(num, 1);
        for (int i = 0; i < length; i++) {
            System.out.print(num[i] + " ");
        }
        System.out.println();
    }
}